﻿using System;
using System.Collections.Specialized;
using UFIDA.U9.BS.Job.RequestClient;
using UFIDA.U9.Cust.Pub.WSLogBE;
using UFIDA.U9.Cust.Pub.WSLogBP;
using UFIDA.U9.Cust.Pub.WSLogBP.Proxy;
using UFIDA.U9.Cust.Pub.WSLogUI;
using UFIDA.U9.UI.PDHelper;
using UFIDA.UBF.Query.CommonService;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.WebControls;

/***********************************************************************************************
 * Form ID: 
 * UIFactory Auto Generator 
 ***********************************************************************************************/

namespace UFIDA.U9.Cust.Pub.WSLogBListUIModel
{
    public partial class WSLogBListUIFormWebPart
    {
        #region Custome eventBind

        //BtnNew_Click...
        private void BtnNew_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnNew_Click_DefaultImpl(sender, e);
        }

        //BtnDelete_Click...
        private void BtnDelete_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnDelete_Click_DefaultImpl(sender, e);
        }

        //BtnOutPut_Click...
        private void BtnOutPut_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnOutPut_Click_DefaultImpl(sender, e);
        }

        //BtnPrint_Click...
        private void BtnPrint_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnPrint_Click_DefaultImpl(sender, e);
        }

        //DDLCase_TextChanged...
        private void DDLCase_TextChanged_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            DDLCase_TextChanged_DefaultImpl(sender, e);
        }

        //OnLookCase_Click...
        private void OnLookCase_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            OnLookCase_Click_DefaultImpl(sender, e);
        }

        //btnSaveCurrentQueryCase_Click...
        private void btnSaveCurrentQueryCase_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            //btnSaveCurrentQueryCase_Click_DefaultImpl(sender, e);
            QryService.SaveQueryCaseColumnsWidth(Action, sender, e);
        }

        //BtnDoRequest_Click...
        private void BtnDoRequest_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            //BtnDoRequest_Click_DefaultImpl(sender, e);
            //重新请求
            DoRequest();
        }

        //BtnShowContent_Click...
        private void BtnShowContent_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            BtnShowContent_Click_DefaultImpl(sender, e);
            //显示请求内容
            DoShowContent();
        }

        //DataGrid1_GridRowDbClicked...
        private void DataGrid1_GridRowDbClicked_Extend(object sender, GridDBClickEventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            DataGrid1_GridRowDbClicked_DefaultImpl(sender, e);
            //显示请求内容
            DoShowContent();
        }

        //MenuDeleteBeforeThreeMonthsLog_Click...
        private void MenuDeleteBeforeThreeMonthsLog_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            MenuDeleteBeforeThreeMonthsLog_Click_DefaultImpl(sender, e);
            //执行删除三个月之前的日志
            DoDeleteBeforeThreeMonthsLog();
        }

        //MenuDeleteBeforeOneYearLog_Click...
        private void MenuDeleteBeforeOneYearLog_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            MenuDeleteBeforeOneYearLog_Click_DefaultImpl(sender, e);
            //执行删除一年之前的日志
            DoDeleteBeforeOneYearLog();
        }

        //MenuDeleteBeforeDateLog_Click...
        private void MenuDeleteBeforeDateLog_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            MenuDeleteBeforeDateLog_Click_DefaultImpl(sender, e);
            //显示选择日期的窗口
            ShowSelectDateWin();
        }

        //OnLookDeleteLogInfo_Click...
        private void OnLookDeleteLogInfo_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            //OnLookDeleteLogInfo_Click_DefaultImpl(sender, e);
            NameValueCollection nameValueCollection = new NameValueCollection();
            nameValueCollection.Add("BPFullName",
                "UFIDA.U9.Cust.Pub.WSLogBP.DeleteDBLogBP");
            nameValueCollection.Add("Application", "3000");
            ShowModalDialog("0766ae8f-47ab-4715-ab27-9dda7660cc6f", "", "696", "488", "", nameValueCollection);
        }

        #region 自定义数据初始化加载和数据收集

        private void OnLoadData_Extend(object sender)
        {
            OnLoadData_DefaultImpl(sender);
        }

        private void OnDataCollect_Extend(object sender)
        {
            OnDataCollect_DefaultImpl(sender);
        }

        #endregion

        #region 自己扩展 Extended Event handler 

        public void AfterOnLoad()
        {
            AfterOnLoad_Qry_DefaultImpl(); //BE列表自动产生的代码
            //选择日期返回
            OnSelectDateReturn();
        }

        public void AfterCreateChildControls()
        {
            AfterCreateChildControls_Qry_DefaultImpl(); //BE列表自动产生的代码
            //初始化确认窗口
            PDFormMessage.ShowConfirmDialog(Page, "是否确认删除三个月前的日志?", "确认", DDBtnClearLog,
                MenuDeleteBeforeThreeMonthsLog);
            PDFormMessage.ShowConfirmDialog(Page, "是否确认删除一年前的日志?", "确认", DDBtnClearLog, MenuDeleteBeforeOneYearLog);
        }

        public void AfterEventBind()
        {
        }

        public void BeforeUIModelBinding()
        {
        }

        public void AfterUIModelBinding()
        {
            AfterUIModelBinding_Qry_DefaultImpl(); //BE列表自动产生的代码
            //新增
            BtnNew.Visible = false;
        }

        #endregion

        #region 方法

        /// <summary>
        ///     重新请求
        /// </summary>
        private void DoRequest()
        {
            Model.ClearErrorMessage();
            WSLogRecord focusedRecord = Model.WSLog.FocusedRecord;
            if (focusedRecord == null) return;
            long wsLogID = focusedRecord.ID;
            DoRequestBPProxy proxy = new DoRequestBPProxy();
            proxy.WSLogID = wsLogID;
            RequestResultDTOData resultData = proxy.Do();
            if (Model.ErrorMessage.hasErrorMessage || Model.ErrorMessage.hasChildMessage) return;
            Action.NavigateAction.Refresh(DataGrid1);
            if (resultData.CallResult != (int) CallResultEnumData.Success)
            {
                IUIModel iModel = Model;
                WSLogRecord record = Model.WSLog.Records.FindByKey(wsLogID) as WSLogRecord;
                Model.WSLog.FocusedRecord = record;
                Model.ErrorMessage.SetErrorMessage(ref iModel, Model.WSLog.EntityFullName,
                    "ID", false, wsLogID, resultData.ErrorMessage);
            }
        }

        /// <summary>
        ///     显示请求内容
        /// </summary>
        private void DoShowContent()
        {
            Model.ClearErrorMessage();
            WSLogRecord focusedRecord = Model.WSLog.FocusedRecord;
            if (focusedRecord == null) return;
            long wsLogID = focusedRecord.ID;
            NameValueCollection nv = new NameValueCollection();
            nv.Add("LogID", wsLogID.ToString());
            const string formID = "37eefa0b-f334-42e2-bc3b-b0ce3c43cb31";
            ShowAtlasModalDialog(formID, "请求内容", "1002", "514", TaskId.ToString(), nv, false, false,
                false);
        }

        #region 按钮

        /// <summary>
        ///     显示选择日期的窗口
        /// </summary>
        private void ShowSelectDateWin()
        {
            Model.ClearErrorMessage();
            //显示选择日期窗口
            WSLogUIHelper.ShowSelectDateWin(this, DateTime.Now.Date);
        }

        /// <summary>
        ///     选择日期返回
        /// </summary>
        private void OnSelectDateReturn()
        {
            DateTime selectDate = WSLogUIHelper.GetReturnSelectDate(this);
            if (selectDate == DateTime.MinValue) return;
            //通过调度删除对接日志
            DoDeleteLogByJob(selectDate);
        }

        /// <summary>
        ///     执行删除三个月之前的日志
        /// </summary>
        private void DoDeleteBeforeThreeMonthsLog()
        {
            DateTime delDate = DateTime.Now.AddMonths(-3);
            delDate = new DateTime(delDate.Year, delDate.Month, 1);
            //通过调度删除服务日志
            DoDeleteLogByJob(delDate);
        }

        /// <summary>
        ///     执行删除一年之前的日志
        /// </summary>
        private void DoDeleteBeforeOneYearLog()
        {
            DateTime delDate = new DateTime(DateTime.Now.Year, 1, 1);
            //通过调度删除服务日志
            DoDeleteLogByJob(delDate);
        }

        /// <summary>
        ///     通过调度删除服务日志
        /// </summary>
        /// <param name="date"></param>
        private void DoDeleteLogByJob(DateTime date)
        {
            DeleteDBLogBPProxy proxy = new DeleteDBLogBPProxy();
            proxy.Date = date;
            RequestSubmit rs = new RequestSubmit();
            //参数applicaiton表示应用模块的代号.
            rs.Application = 3000L;
            //参数priority表示调度执行的优先级,1表示最高优先级
            rs.Priority = 1;
            //参数bpfullname表示服务的全名,包括命名空间
            rs.BPFullName = "UFIDA.U9.Cust.Pub.WSLogBP.DeleteDBLogBP";
            //表示需要调度执行的应用,及调度具体执行的应用
            rs.BPAgentObj = proxy;
            //表示请求执行的应用代码,即具体的服务名称
            rs.RequestCode = string.Format("DeleteDBLog_{0}", date.ToShortDateString());
            //在表示请求执行的服务名称,即在请求监控中看到的进程名称
            string requestName = string.Format("删除服务日志:{0}", date.ToShortDateString());
            rs.RequestName = requestName;
            //任何语言都可以的描述
            rs.RequestDescription = requestName;
            // 是否成功执行不能重新执行
            rs.IsSuccessCanNotReStart = true;
            //表示任务执行的时限,以秒为单位,即指定秒后开始进行调度执行
            rs.SchedulerProject = new PatternRunOnce().PatternInstant();
            //参数表示 提交到job进程里,可以保证在设定时间进行应用程序的执行
            rs.Submit();
            ShowWindowStatus("删除服务日志操作提交成功！");
        }

        #endregion

        #endregion

        #endregion
    }
}